const Base = require('../base.js');

module.exports = class extends Base {
  /**
   *
   * @api {GET} /userinfo/{id} 获取用户信息接口
   * @apiName 获取用户
   * @apiGroup userinfo
   * @apiVersion  1.0.0
   *
   * @apiName GetUserInfo
   *
   * @apiParam {Number} id User unique ID
   *
   * @apiSuccess (200) {string} wechat 微信号
   *
   * @apiSuccess (200) {String} entrytime 入职时间
   *
   * @apiSuccess (200) {Number} status 状态0为离职，1为在职
   *
   * @apiSuccess (200) {String} content 工作内容
   *
   * @apiSuccess (200) {String} sign 签名
   *
   * @apiSuccess (200) {String} home 主页
   *
   * @apiSuccess (200) {String} share 分享
   *
   * @apiSuccess (200) {String} realname 真实姓名
   *
   * @apiSuccess (200) {String} sex 性别
   *
   * @apiSuccess (200) {String} age 年龄
   *
   * @apiSuccess (200) {String} birthday 出生日期
   *
   * @apiSuccess (200) {String} telephone 电话号码
   *
   * @apiSuccess (200) {String} email 电子邮件
   *
   * @apiSuccess (200) {String} org 对应的机构名
   *
   * @apiSuccess (200) {String} school 毕业院校
   *
   * @apiSuccessExample {json} Success-Response:
   * {
   *     "wechat" : "fdsas",
   *     "entrytime": "",
   *     "status": "",
   *     "content": "",
   *     "sign": "",
   *     "home": "",
   *     "share": "",
   *     "realname": "侯斌飞",
   *     "sex": null,
   *     "age": null,
   *     "birthday": null,
   *     "telephone": "15829669905",
   *     "email": "houbf@bsar.com.cn",
   *     "school": "sss",
   *     "org": "业务一组"
   * }
   *
   *
   */
  async getAction() {
    try {
      const id = this.get('id');
      const user = think.model('user');
      const userLocal = think.model('user', 'mysqlLocal');
      const { wechat = '', entrytime = '', status = '', content = '', sign = '', home = '', share = '' } = await userLocal.where({ user_id: id }).field('wechat, entrytime, status, content, sign, home, share').find();
      const userInfo = await user.where({ id }).field('realname, sex, age, birthday, telephone, email, school').find();
      const { getUserOrgName } = think.service('userinfo/user');
      const orgName = await getUserOrgName(id);
      const userData = await user.where({ id }).field('id, realname, pictureid').select();
      const { getUserImg } = await think.service('userinfo/user');
      const userDataWithImgUrl = await getUserImg(userData);
      this.body = Object.assign({}, { wechat, entrytime, status, content, sign, home, share }, userInfo, { org: orgName, pictureUrl: userDataWithImgUrl[0].pictureUrl });
    } catch (error) {
      this.fail('502');
      think.logger.debug(error);
    }
  }
  /**
     *
     * @api {PUT} /userinfo/{id} 更新用户信息
     * @apiName 更新用户
     * @apiGroup userinfo
     * @apiVersion  1.0.0
     *
     * @apiParam {Number} id User unique ID
     *
     * @apiParam {String} wechat 微信号
     *
     * @apiParam {String} entrytime 入职时间
     *
     * @apiParam {String} status 状态0为离职，1为在职
     *
     * @apiParam {String} content 工作内容
     *
     * @apiParam {String} sign 签名
     *
     * @apiParam {String} home 主页
     *
     * @apiParam {String} share 分享
     *
     * @apiParamExample  {type} Request-Example:
     * {
     *     "wechat" : "12",
     *     "entrytime": "",
     *     "status": "",
     *     "content": "",
     *     "sign": "",
     *     "home": "",
     *     "share": "",
     * }
     *
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     *    msg: ""
     * }
     *
     *
     */
  async putAction() {
    try {
      const id = this.get('id');
      const params = this.post();
      const userLocal = think.model('user', 'mysqlLocal');
      const userExpand = await userLocal.where({ user_id: id }).find();
      if (userExpand.user_id) {
        await userLocal.where({ user_id: id }).update(params);
      } else {
        params.user_id = id;
        await userLocal.add(params);
      }
      this.body = params;
    } catch (error) {
      this.fail('502');
      think.logger.debug(error);
    }
  }
};
